//
//  HZJBaseViewController.swift
//  HZJNavigationView
//
//  Created by HZJ on 2017/2/20.
//  Copyright © 2017年 HZJ. All rights reserved.
//

import UIKit
class HZJBaseViewController: UIViewController {
    
    override var title: String?{
        didSet{
            myNavigationBar.myTitleLable.text = title
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.navigationController?.navigationBar.isHidden = true
        self.view.addSubview(myNavigationBar)
        myNavigationBar.frame = self.navigationController!.navigationBar.frame
        myNavigationBar.myLeftBtn.addTarget(self, action: #selector(clickTheLeftBtn), for: UIControlEvents.touchUpInside)        
        self.setUpUI()
    }
    
    /**
     *中部按钮(测试用)
     */
    lazy var centerBtn:UIButton = UIButton()
    
    private func setUpUI() {
        //        let centerBtn = UIButton()
        centerBtn.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        centerBtn.center = self.view.center
        centerBtn.backgroundColor = .gray
        centerBtn.addTarget(self, action: #selector(clickTheBtn), for: UIControlEvents.touchUpInside)
        self.view.addSubview(centerBtn)
    }
    @objc func clickTheBtn() {
        
    }
    
    //MARK: - 导航栏按钮点击方法
    @objc func clickTheLeftBtn() {
        self.navigationController?.popViewController(animated: true)
    }
    func clickTheRightBtn() {
        
    }
    
    
    //MARK: - 懒加载导航栏按钮
    lazy var myNavigationBar:HZJNavigationBar = {
        let nav = HZJNavigationBar()
        return nav
    }()
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
//先前的写法
//var avd = 0
class HZJBaseViewControllerOne: UIViewController {
    
    override var title: String?{
        didSet{
//            myTitleLable.text = "\(avd)"
//            avd += 1
            myTitleLable.text = title

        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.navigationController?.navigationBar.isHidden = true

        self.view.addSubview(myNavigationBar)
        let firstY = UIApplication.shared.statusBarFrame.size.height
        let oldFrame = self.navigationController!.navigationBar.frame
        let navFrame = CGRect(x: oldFrame.minX, y: 0, width: oldFrame.width, height: oldFrame.height + firstY)
        myNavigationBar.frame = navFrame
        
        
        if self.navigationController?.childViewControllers.count != 1 {
            myNavigationBar.addSubview(myLeftBtn)
            myLeftBtn.frame = CGRect(x: 15, y: firstY + (oldFrame.height - 40) * 0.5, width: 40, height: 40)
            myLeftBtn.addTarget(self, action: #selector(clickTheLeftBtn), for: UIControlEvents.touchUpInside)
            myLeftBtn.setTitle("back", for: UIControlState.normal)
        }
        
        
        myNavigationBar.addSubview(myRightBtn)
        myRightBtn.frame = CGRect(x: oldFrame.width - 40 - 15, y:firstY + (oldFrame.height - 40) * 0.5, width: 40, height: 40)
        myRightBtn.setTitle("right", for: UIControlState.normal)
        myRightBtn.addTarget(self, action: #selector(clickTheRightBtn), for: UIControlEvents.touchUpInside)
        
        myNavigationBar.addSubview(myTitleLable)
        myTitleLable.frame = CGRect(x: 15 + 40 + 5, y: oldFrame.minY, width: oldFrame.width - (15 + 40 + 5) * 2, height: oldFrame.height)
        
        self.setUpUI()
        self.myNavigationBar.titleTextAttributes = [:]
    }

    /**
     *中部按钮(测试用)
     */
    lazy var centerBtn:UIButton = UIButton()

    private func setUpUI() {
//        let centerBtn = UIButton()
        centerBtn.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        centerBtn.center = self.view.center
        centerBtn.backgroundColor = .random
        centerBtn.addTarget(self, action: #selector(clickTheBtn), for: UIControlEvents.touchUpInside)
        self.view.addSubview(centerBtn)
    }
    @objc func clickTheBtn() {

    }
    
    //MARK: - 导航栏按钮点击方法
    @objc func clickTheLeftBtn() {
        self.navigationController?.popViewController(animated: true)
    }
    @objc func clickTheRightBtn() {
        
    }
    
    
    
    //MARK: - 懒加载导航栏按钮
    lazy var myNavigationBar:UINavigationBar = {
        let y = UIApplication.shared.statusBarFrame.size.height
        let f = self.navigationController!.navigationBar.frame
        let nav = UINavigationBar(frame: CGRect(x: f.minX, y: 0, width: f.width, height: f.height + y))
        return nav
    }()
    /**
     *导航栏左侧按钮，若需添加点击方法，应重写“clickTheLeftBtn”方法
     */
    lazy var myLeftBtn:UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.black, for: UIControlState.normal)
        btn.titleLabel?.textAlignment = .center
        return btn
    }()
    
    /**
     *导航栏右侧按钮，若需添加点击方法，应重写“clickTheRightBtn”方法
     */
    lazy var myRightBtn:UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.black, for: UIControlState.normal)
        btn.titleLabel?.textAlignment = .center
        return btn
    }()
    /**
     *导航栏标题
     */
    lazy var myTitleLable:UILabel = {
        let tv = UILabel()
        tv.textAlignment = .center
//        tv.font = UIFont.systemFont(ofSize: 17)
        tv.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight(rawValue: 0.2))
        return tv
    }()
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

